<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>登录</title>
    <link href="http://libs.baidu.com/bootstrap/3.0.3/css/bootstrap.min.css" rel="stylesheet" />
    <link rel="stylesheet" href="./css/Login.css">
    <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
    <script src="http://libs.baidu.com/bootstrap/3.0.3/js/bootstrap.min.js"></script>

</head>
<body>
    <div class="Login__Container">
        <div class="Login__form">
            <h2 class="Login__form__title">登录</h2>
            <input type="text" id="username" class="form-control" placeholder="Username"><br>
            <input type="password" id="password" class="form-control" placeholder="Password"><br>
            <button type="button" id="btnLogin" class="btn btn-primary btn-sm">Login</button><br>
            Or &nbsp;<a href="#">register now!</a>
        </div>
    </div>

    <script type="text/javascript">
        $(document).ready(function () {
            //点击登录
            $("#btnLogin").click(function () {
                //1.获取到用户输入的用户名和密码
                var username = $("#username").val();
                var password = $("#password").val();
                //用户名和密码不能为空
                // console.log(username);
                // console.log(password);
                if(username == "" || password == ""){
                    alert("用户名或者密码不能为空！");
                    return ;
                }
                //将用户名密码传入后台验证
                $.ajax({
                    url: '/user/check_user_login',
                    type: 'POST',
                    data: {
                        "username": username,
                        "password": password,
                    },
                    success:function (res) {
                        //console.log(res);
                        if(res == 1){
                            //错误提示
                            alert("用户名不存在！");
                            //用户名输入框获得焦点，方便用户从新输入
                            $("#username").focus();
                        }else if (res == 2){
                            //错误提示
                            alert("用户名密码错误！");
                            //密码框获得焦点，方便用户输入
                            $("#password").focus();
                            $("#password").val("");
                        }else if(res == 0){
                            //alert("登录成功！");
                            //跳转到员工管理页面
                            window.location.href = "/index";
                        }

                    },
                    error:function (err) {
                        console.log(err);
                    }
                });
            });
        });
    </script>
</body>
</html>